import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.math.RandomUtils;

import com.rongzhong.time.annotation.Minute;
import com.rongzhong.time.base.Task;
import com.rongzhong.time.util.RedisUtil;

/*
 * ETask.java
 * Copyright (c) 2011,融众网络技术有限公司(www.11186.com)
 * All rights reserved.
 * ---------------------------------------------------------------------
 * 2011-11-22 Created
 */


/**
 * 
 * @author wufei
 * @version 1.0 2011-11-22
 * @since 1.0
 * @see .ETask.java
*/
public class ETask  implements Task{
//	private String url = "http://trading.5173.com/search/54c771af3b304a2b9d939069951dacb0.shtml?raceid=36a6903f16544120927817ac70b3e6e2&cate=f3cb5c6b024b4183b7ed9d011f214191&sort=MoneyAveragePrice_ASC&basictype=1&ga=0496a96604cd4930a6edfd82daafde6a&gs=ded8b30b4329470ca42d224c7138b4dc";
	private String url = "http://trading.5173.com/search/87c1026b419e472c93a84776a3e7cf48.shtml?ga=7b963ce0612b488b8da74fa78c062fe8&gs=aa71f02de5ac400b9f199cd24d9c959d&cate=ed665471a3c44ef4bdb9d1989f048b4d&sort=UnitPrice_ASC";
	private static final SimpleDateFormat timeFormat = new SimpleDateFormat("MM-dd HH:mm");
	
	private RedisUtil u = new RedisUtil();
	@Minute
	public void a() throws HttpException, IOException{
		PostMethod post = new PostMethod(url);
		post.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
		
		HttpClient client = new HttpClient();
		client.executeMethod(post);
		String s = post.getResponseBodyAsString();
		
		int j = 0;
		String zj="";
		String dj="";
		int start = s.indexOf("<li class=\"pr\"><strong>");
		while (start != -1) {
			System.out.print(timeFormat.format(new Date())+"，");
			s = s.substring(start+23);
			int end = s.indexOf("</strong>");
			zj = s.substring(0, end);
			start = s.indexOf("<li class=\"pr\">");
			s = s.substring(start+15);
			end = s.indexOf("</li>");
			dj = s.substring(0, end);
			
			System.out.println(zj+"，"+dj);
			if(j<=1){
				float t = Float.parseFloat(zj);
				if(j<=0){
					u.set("zj4",( t*10000+RandomUtils.nextInt(100))+"");
				}
				u.set("dj3", dj.replace("元/万金币", ""));
			}
			j++;
			 start = s.indexOf("<li class=\"pr\"><strong>");
		}
		System.out.println("-----------------------------------");
		
	}

	public void initialize() {
		// TODO Auto-generated method stub
		
	}

}
